% TEST_ID: LANG-EXPAND-0117
% CATEGORY: language/string
% 测试 string 的分割

function test_string_split()
x = 'hello world';
y = split(x);
assert(strcmp(y{1}, 'hello'));
assert(strcmp(y{2}, 'world'));

names = {'Mary Butler';
    'Santiago Marquez';
    'Diana Lee'};
y = split(names);
ret = {'Mary'        'Butler'
    'Santiago'    'Marquez'
    'Diana'       'Lee'   };
for i = 1 : length(ret)
    assert(strcmp(y, ret));
end

myPath = '/Users/jdoe/My Documents/Examples';
myFolders = split(myPath,'/');
myNewPath = join(myFolders,'\');
ret = {'\Users\jdoe\My Documents\Examples'};
assert(strcmp(myNewPath, ret));

str = 'A horse! A horse! My kingdom for a horse!';
newStr = split(str,{' ','!'});
ret = {'A'
    'horse'
    ''
    'A'
    'horse'
    ''
    'My'
    'kingdom'
    'for'
    'a'
    'horse'
    ''};
for i = 1 : length(ret)
    assert(strcmp(newStr, ret));
end

patients = {'LastName,Age,Gender,Height,Weight';
    'Adams,47,F,64,123';
    'Jones,,,68,175';
    'King,,M,66,180';
    'Smith,38,F,63,118'};
patients = split(patients,',');
ret = {'LastName'    'Age'    'Gender'    'Height'    'Weight'
    'Adams'       '47'     'F'         '64'        '123'
    'Jones'       ''       ''          '68'        '175'
    'King'        ''       'M'         '66'        '180'
    'Smith'       '38'     'F'         '63'        '118' };
for i = 1 : length(ret)
    assert(strcmp(patients, ret));
end

names = {'Mary Butler';
    'Santiago Marquez';
    'Diana Lee'};
splitNames = split(names);
ret = {'Mary'      'Santiago'    'Diana'
    'Butler'    'Marquez'     'Lee'  };

str = 'bacon, lettuce, and tomato';
[newStr,match] = split(str,{'and',',',' '});
ret1 = {'bacon'
    ''
    'lettuce'
    ''
    ''
    ''
    'tomato'};
ret2 = {','
    ' '
    ','
    ' '
    'and'
    ' '};
for i = 1 : length(ret1)
    assert(strcmp(newStr, ret1));
end
for i = 1 : length(ret2)
    assert(strcmp(match, ret2));
end

str = 'abc';
[newStr, match] = split(str,{'', ''});
ret1 = {''; 'a'; 'b'; 'c'; ''};
ret2 = {''; ''; ''; ''};

for i = 1 : length(ret1)
    assert(strcmp(newStr, ret1));
end

for i = 1 : length(ret2)
    assert(strcmp(match, ret2));
end

[newStr, match] = split(str,'');
ret1 = {''; 'a'; 'b'; 'c'; ''};
ret2 = {''; ''; ''; ''};

for i = 1 : length(ret1)
    assert(strcmp(newStr, ret1));
end

for i = 1 : length(ret2)
    assert(strcmp(match, ret2));
end

str = ['a' char(10) ' ', 'b'];
[newStr, match] = split(str);
ret1 = {'a'; 'b'};
ret2 = {[char(10) ' ']};
assert(strcmp(newStr, ret1));
assert(strcmp(match, ret2));

[newStr, match] = split('','abc');
ret1 = {''};
ret2 = {};
assert(strcmp(newStr, ret1));
assert(isempty(match));


names = {'Mary Butler';
    'Santiago Marquez';
    'Diana Lee'};
splitNames = split(names,' ',1)
result ={'Mary'      'Santiago'    'Diana'
    'Butler'    'Marquez'     'Lee'  };
assert(isequal(splitNames, result));

newStr = split('sssss', 'ss');
assert(isequal(newStr, {''; '';'s'}));